﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GameFramework;
using UnityGameFramework.Runtime;

public class GuessControl : MonoBehaviour
{    
    //待机静止画面
    //待机轮播画面
    //快速轮播画面
    //停止中画面
    //停止了的画面
    //结果画面

    public GameObject Prefab;
    public float Speed;

    public float MaxPosiX = 10;

    public float Offset = 1.5f;

    public GuessQueue<GameObject> Items;



    public Image StaticImage;

    void Start()
    {
        //创建并启动状态机
        GameEntry.GetComponent<FsmComponent>()
            .CreateFsm(this,
            new GuessState_idle(),
            new GuessState_QuaklyMove(),
            new GuessState_Resutls(),
            new GuessState_Stoped(),
            new GuessState_Stoping(),
            new GuessState_UpdateIdle())
            .Start<GuessState_idle>();


        Items = new GuessQueue<GameObject>();

        for (int i = 0; i < 10; i++)
        {
            AddItem(Instantiate<GameObject>(Prefab));
        }
    }
    private void FixedUpdate()
    {
        CheckPosition();

        foreach (var item in Items.GetQueue)
        {
            Vector3 op = item.transform.localPosition;
            op.x += Speed * Time.fixedDeltaTime;
            item.transform.localPosition = op;
        }


    }


    
    private void CheckPosition()
    {
        while (Items.Count>0)
        {
            GameObject item = Items.Peek();
            if (item.transform.localPosition.x > MaxPosiX)
            {
                Items.DeQueue();

                AddItem(item);
            }
            else
            {
                break;
            }
        }
    }

    public void AddItem(GameObject item)
    {
        item.transform.SetParent(transform);

        if (Items.Count<=0)
        {
            item.transform.localPosition = Vector3.zero;

        }
        else
        {
            Vector3 lp = Items.LastItem.transform.position;
            lp.x -= Offset;
            item.transform.localPosition = lp;
        }

        Items.EnQueue(item);
    }

}
public class GuessQueue<T>
{
    private readonly Queue<T> Items;

    public T LastItem { get; private set; }

    public Queue<T> GetQueue { get { return Items; } }

    public int Count { get { return Items.Count; } }


    public GuessQueue()
    {
        Items = new Queue<T>();
    }
    public T Peek()
    {
        if (Items!=null && Items.Count>0)
        {
            return Items.Peek();
        }
        
        throw new System.Exception("Peek Queue Is Null");
    }

    public T DeQueue()
    {
        T item;
        if (Items != null && Items.Count > 0)
        {
            item = Items.Dequeue();

            if (Items.Count<=0)
            {
                LastItem = default;
            }


            return item;
        }
        

        throw new System.Exception("Dequeue Queue Is Null");
    }


    public void EnQueue(T v)
    {
        if (Items == null)
        {
            throw new System.Exception("EnQueue Queue Is Null");
        }
        if (v == null)
        {
            throw new System.Exception("EnQueue value is null");
        }


        Items.Enqueue(v);
        LastItem = v;
    }


}